package com.itheima.utils.sort;

import cn.hutool.core.util.RandomUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.Random;

public class 冒泡排序 {


    public static void main(String[] args) {

        int[] arr = new int[]{9,7,-2,0,6};


//        int ll = 80;
//        arr = new int[ll];
//        for (int i = 0; i < ll; i++) {
//            arr[i] = new Random().nextInt(ll);
//        }


        System.out.println(new Date());
        //7,-2,0,6,9  比4次
        //-2,0,6,7,9  比3次
        //-2,0,6,7,9  比2次
        //-2,0,6,7,9  比1次
        //外面循环n-1次
        //里面循环n - 1 再减去外层已经循环的次数
        boolean f = false;
        for (int j = 0; j < arr.length - 1; j++) {
            for (int i = 0; i < arr.length - 1 - j ; i++) {
                if(arr[i] < arr[i+1]){
                    f = true;//发生了交换
                    int tmp = 0;
                    tmp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = tmp;
                }
            }

            if(f == false){
                break;
            } else {
                f = false;
            }

            System.out.println(Arrays.toString(arr) + ",循环了：" + (j + 1));
        }
        System.out.println(new Date());

//        System.out.println(Arrays.toString(arr));





    }


}
